package com.evolved.automata.android.bots.speechbot;
import android.content.res.*;
import android.content.*;
import java.util.*;


public class MacroProcessor 
{
	InferenceApp parent;
	List<MapTransformer> macroProcessors;
	Resources resources;
	
	public MacroProcessor(InferenceApp theParent)
	{
		parent=theParent;
		resources=parent.getResources();
		macroProcessors = new LinkedList<MapTransformer>();
		setAllMacros();
	}
	
	private void setAllMacros()
	{
		String processNam="DateSetter";
		MapTransformer transformer;
		transformer = new MapTransformAdapter(null,".fulldate",true)
		{

			@Override
			public String processKeyValue(String keyName) {
				Calendar calendar = (Calendar)GregorianCalendar.getInstance();
				
				if (keyName.equals("YEAR"))
					return ""+calendar.get(Calendar.YEAR);
				else if (keyName.equals("DAY_OF_WEEK"))
					return StandardTools.mapDatepartToStringName(Calendar.DAY_OF_WEEK, calendar.get(Calendar.DAY_OF_WEEK));
				else if (keyName.equals("MONTH_NAME"))
					return StandardTools.mapDatepartToStringName(Calendar.MONTH, calendar.get(Calendar.MONTH)+1);
				else if (keyName.equals("WEEK_OF_MONTH"))
					return ""+calendar.get(Calendar.MONTH);
				else if (keyName.equals("FULL_DATE"))
					return StandardTools.getCurrentDateString();
				else if (keyName.equals("SEASON_NAME"))
					return StandardTools.MONTH_NUMBER_TO_SEASON_NAME_MAP.get(calendar.get(Calendar.MONTH)+1);
				else if (keyName.equals("MONTH_NUMBER"))
					return ""+calendar.get(Calendar.MONTH)+1;
				else if (keyName.equals("DAY_OF_MONTH"))
					return ""+calendar.get(Calendar.DATE);
				return "";
			}

			@Override
			public String[] getKeysToAdd() {
				
				return new String[]{"DAY_OF_WEEK","MONTH_NAME","YEAR","WEEK_OF_MONTH","FULL_DATE","SEASON_NAME","MONTH_NUMBER","DAY_OF_MONTH"};
			}

			@Override
			public String[] getKeysToRemove() 
			{	
				return new String[0];
			}
			
		};
		
		macroProcessors.add(transformer);
		
		transformer = new MapTransformAdapter(null,".Date",true)
		{

			@Override
			public String processKeyValue(String keyName) {
				if (keyName.equals("FULL_DATE"))
					return StandardTools.getCurrentDateString();
				else 
					return "";
			}

			@Override
			public String[] getKeysToAdd() {
				
				return new String[]{"FULL_DATE"};
			}

			@Override
			public String[] getKeysToRemove() 
			{	
				return new String[0];
			}
			
		};
		
		macroProcessors.add(transformer);
		
	}
	
	public Hashtable<String,String> transformOutputMap(Hashtable<String,String> keyMap)
	{
		Hashtable<String,String> out = (Hashtable<String,String>)keyMap.clone();
		for (MapTransformer transformer:macroProcessors)
		{
			out = transformer.transformMap(out);
		}
		return out;
	}
	 
}
